package com.sevlet.statistics;

import com.Bean.*;
import com.dao.StatisticsDao;
import com.dao.StatisticsThreeDao;
import com.dao.StatisticsTwoDao;
import com.dao.StatisticsfourDao;
import com.dao.impl.StatisticsDaoThreelmpl;
import com.dao.impl.StatisticsDaoTwolmpl;
import com.dao.impl.StatisticsDaofourlmpl;
import com.dao.impl.StatisticsDaolmpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import com.sevlet.BaseSevlet;
import com.util.JdbcUtil;
import com.util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import java.util.List;
@WebServlet("/StatisticsSevlet")
public class StatisticsSevlet extends BaseSevlet {
    
    public void queryOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StatisticsDao statisticsDao = new StatisticsDaolmpl();
        RoomList roomList = new RoomList();
        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;
        Fddate fddate=new Fddate();

        String[] times = req.getParameterValues("times[]");

      if (times!=null){

          fddate.setFristDay(times[0]);
          fddate.setLastDay(times[1]);
      }

        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            List<RoomList> roomLists = statisticsDao.queryDataone(roomList,fddate);


            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(roomLists);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }finally {
            out.print(gson.toJson(jsonUtil));
        }

    }

    public void queryTwo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StatisticsTwoDao statisticsTwoDao = new StatisticsDaoTwolmpl();
        RoomListTwo roomListTwo = new RoomListTwo();

        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;
        Fddate fddate=new Fddate();

        String[] times = req.getParameterValues("times[]");

        if (times!=null){

            fddate.setFristDay(times[0]);
            fddate.setLastDay(times[1]);
        }

        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            List<RoomListTwo> roomLists = statisticsTwoDao.queryDatatwo(roomListTwo,fddate);
            System.out.println(roomLists.toString());
            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(roomLists);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }finally {
            out.print(gson.toJson(jsonUtil));
        }

    }

    public void querythree(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StatisticsThreeDao statisticsThreeDao = new StatisticsDaoThreelmpl();
        RoomListThree roomListThree = new RoomListThree();
        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;
        Fddate fddate=new Fddate();

        String[] times = req.getParameterValues("times[]");
        System.out.println(times);

        if (times!=null){
            fddate.setFristDay(times[0]);
            fddate.setLastDay(times[1]);
        }

        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            List<RoomListThree> roomListsThree = statisticsThreeDao.queryDatathree(roomListThree,fddate);


            //得到有多少件房
            String sql="select count(*) from room_id";
            Double rooms = JdbcUtil.queryRooms(sql);


            jsonUtil.setCode(0);
            jsonUtil.setMsg(String.valueOf(rooms));

            jsonUtil.setData(roomListsThree);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }finally {
            out.print(gson.toJson(jsonUtil));
        }




    }

    public void queryfour(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StatisticsfourDao statisticsfourDao = new StatisticsDaofourlmpl();
        RoomListFour roomListFour = new RoomListFour();
        resp.setContentType("text/html;charset=utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Gson gson = gsonBuilder.create();
        PrintWriter out = null;
        Fddate fddate=new Fddate();

        String[] times = req.getParameterValues("times[]");//时间
        String select = req.getParameter("select");//选择查询
        String input1 = req.getParameter("input");//输入

        if (times!=null){
            fddate.setFristDay(times[0]);
            fddate.setLastDay(times[1]);
        }
        if (select!=null&&input1!=null){
            roomListFour.setSelect(select);
            roomListFour.setInput(input1);
        }


        try {
            out = resp.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            List<RoomListFour> roomListsFour = statisticsfourDao.queryDatafour(roomListFour,fddate);



            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");
            jsonUtil.setData(roomListsFour);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }finally {
            out.print(gson.toJson(jsonUtil));
        }


    }

    }
